package com.dream.libxrec.utils;

import java.nio.ByteBuffer;

public class ArrayUtils {
    public static int toShortLittle(byte[] src,short[] des,
                                       int size,boolean isLittle) {

        int count = size/2;

        if (isLittle){
            for (int i = 0; i < count; i++) {
                des[i] =
                        (short) (src[i * 2+1] << 8 | src[2 * i] & 0xff);
            }
        }else {
            for (int i = 0; i < count; i++) {
                des[i] =
                        (short) (src[i * 2] << 8 | src[2 * i+1] & 0xff);
            }
        }

        return count;
    }


    public static String toHexString(byte[] data,int offset,int size){
        StringBuilder hexArray = new StringBuilder();
        for (int i = offset; i < size; i++) {
            String hex = Integer.toHexString(data[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            hexArray.append(hex).append(" ");
        }
        return hexArray.toString();
    }

    public static String toHexString(ByteBuffer dataBuffer, int offset, int size) {
        StringBuilder hexArray = new StringBuilder();
        for (int i = offset; i < size; i++) {
            byte byteValue = dataBuffer.get(i);
            String hex = Integer.toHexString(byteValue & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            hexArray.append(hex).append(" ");
        }
        return hexArray.toString();
    }
}
